package com.ocm.commerceinteraction.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonSyntaxException;
import com.ocm.commerceinteraction.GlobalApplication;
import com.ocm.commerceinteraction.R;
import com.ocm.commerceinteraction.config.RequestAddr;
import com.ocm.commerceinteraction.model.BaseModel;
import com.ocm.commerceinteraction.model.CenterMessage;
import com.ocm.commerceinteraction.model.SearchUserList;
import com.ocm.commerceinteraction.util.helper.DBHelper;
import com.ocm.commerceinteraction.util.helper.LogHelper;
import com.ocm.commerceinteraction.util.http.AjaxCallBack;
import com.ocm.commerceinteraction.util.loading.Loading;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * 课程计划
 *
 * @author zqy
 */
public class MessageListAdapter extends BaseAdapter {

    private Context mContext;
    LayoutInflater inflater;
    List<CenterMessage> centerMessageList;

    public MessageListAdapter(Context context, List<CenterMessage> centerMessageList) {
        mContext = context;
        inflater = LayoutInflater.from(context);
        this.centerMessageList = centerMessageList;
    }


    public final class ViewHolder {
        public ViewHolder() {
            // TODO Auto-generated constructor stub
        }

        public ImageView headImage; // 头像
        public TextView userName;// 用户名
        public TextView refuseBtn;// 添加按钮
        public TextView allowBtn;// 添加按钮
        public TextView applayTitle;
        public TextView notifyTitle;
    }

    @SuppressWarnings("null")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.message_list_item, null);
            holder.headImage = (ImageView) convertView.findViewById(R.id.head);
            holder.userName = (TextView) convertView.findViewById(R.id.title);
            holder.refuseBtn = (TextView) convertView.findViewById(R.id.tv_refuse_btn);
            holder.allowBtn = (TextView) convertView.findViewById(R.id.tv_allow_btn);
            holder.applayTitle = (TextView) convertView.findViewById(R.id.tv_applay);
            holder.notifyTitle = (TextView) convertView.findViewById(R.id.tv_nofify);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.userName.setText(centerMessageList.get(position).getContent());
//        Picasso.with(mContext).load(RequestAddr.baseUrl + searchUserList.getData().getList().get(position).getIcon()).into(holder.headImage);
        if (centerMessageList.get(position).getTypeFlag() == 0) {
            holder.applayTitle.setVisibility(View.VISIBLE);
            holder.notifyTitle.setVisibility(View.GONE);
            holder.allowBtn.setVisibility(View.VISIBLE);
            holder.refuseBtn.setVisibility(View.VISIBLE);
        } else if (centerMessageList.get(position).getTypeFlag() == 1) {
            holder.applayTitle.setVisibility(View.GONE);
            holder.notifyTitle.setVisibility(View.VISIBLE);
            holder.allowBtn.setVisibility(View.GONE);
            holder.refuseBtn.setVisibility(View.GONE);
        } else {
            if (centerMessageList.get(position).getType().equals("2") ||
                    centerMessageList.get(position).getType().equals("5") ||
                    centerMessageList.get(position).getType().equals("8")) {
                holder.allowBtn.setVisibility(View.VISIBLE);
                holder.refuseBtn.setVisibility(View.VISIBLE);
            } else {
                holder.allowBtn.setVisibility(View.GONE);
                holder.refuseBtn.setVisibility(View.GONE);
            }
            holder.applayTitle.setVisibility(View.GONE);
            holder.notifyTitle.setVisibility(View.GONE);
        }

        CenterMessage centerMessage = centerMessageList.get(position);
        holder.allowBtn.setTag(centerMessage);
        holder.allowBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogHelper.i("allowBtn click");
                CenterMessage centerMessage = (CenterMessage) v.getTag();
                allowRequest(centerMessage);
            }
        });

        holder.refuseBtn.setTag(centerMessage);
        holder.refuseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogHelper.i("refuseBtn click");
                CenterMessage centerMessage = (CenterMessage) v.getTag();
                refuseRequest(centerMessage);

            }
        });

        return convertView;
    }

    /**
     * 同意添加好友
     */
    void allowRequest(CenterMessage centerMessage) {
        String requestUrl = "";
        if (centerMessage.getType().equals("2")) {
            requestUrl = RequestAddr.allowAdd +
                    "?friend_uniq_id=" + centerMessage.getSender();
        } else if (centerMessage.getType().equals("5") || centerMessage.getType().equals("8")) {
            requestUrl = RequestAddr.allowAddGroup +
                    "?group_uniq_id=" + centerMessage.getGroup_uniq_id() +
                    "&sender=" + centerMessage.getSender();
        } else {
            return;
        }
        DBHelper.updateCenterMsgState(centerMessage);
        GlobalApplication.xtHttp.get(requestUrl, new AjaxCallBack<Object>() {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);
                Loading.stopLoading();
                dealBaseData(o);
                notifyDataSetChanged();
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                Loading.stopLoading();
            }
        });
    }

    /**
     * 拒绝添加好友
     */
    void refuseRequest(CenterMessage centerMessage) {
        String requestUrl = "";
        if (centerMessage.getType().equals("2")) {
            requestUrl = RequestAddr.refuseAdd +
                    "?friend_uniq_id=" + "";
        } else if (centerMessage.getType().equals("5") || centerMessage.getType().equals("8")) {
            requestUrl = RequestAddr.refuseAddGroup +
                    "?group_uniq_id=" + centerMessage.getGroup_uniq_id() +
                    "&sender=" + centerMessage.getSender();
        } else {
            return;
        }
        DBHelper.updateCenterMsgState(centerMessage);
        GlobalApplication.xtHttp.get(requestUrl, new AjaxCallBack<Object>() {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);
                Loading.stopLoading();
                notifyDataSetChanged();
                dealBaseData(o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                Loading.stopLoading();
            }
        });
    }

    /**
     * 处理返回基本结构信息数据
     *
     * @param object  返回json结构
     */
    BaseModel baseModel;

    protected void dealBaseData(Object object) {
        try {
            baseModel = GlobalApplication.gson.fromJson(object.toString(), BaseModel.class);
        } catch (JsonSyntaxException e) {
            baseModel = GlobalApplication.gson.fromJson(object.toString(), BaseModel.class);
        }
        Toast.makeText(mContext, baseModel.getMsg(), Toast.LENGTH_SHORT).show();
    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return centerMessageList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
}
